package com.gk.javaservice.controller;

import com.gk.javaservice.common.ServerResponse;
import com.gk.javaservice.po.AdminRoleUsers;
import com.gk.javaservice.service.AdminRoleUsersService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/AdminRoleUsers")
public class AdminRoleUsersController {

    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private AdminRoleUsersService adminRoleUsersService;

    //添加
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/add")
    public ServerResponse<?> add(@RequestBody AdminRoleUsers t) {
        log.info(t);
        try {
            adminRoleUsersService.add(t);
            return ServerResponse.createBySuccess(t.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("值重复");
        }


    }

    //根据ID删除1个
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/delete")
    public ServerResponse<?> delete(@RequestBody AdminRoleUsers t) {
        log.info("id:" + t.getUserId());
        adminRoleUsersService.delete(t.getUserId());
        return ServerResponse.createBySuccess(t.getUserId());

    }

    //修改信息
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/update")
    public ServerResponse<?> update(@RequestBody AdminRoleUsers t) {
        log.info("id:" + t.getUserId());

        try {
            adminRoleUsersService.update(t);
            return ServerResponse.createBySuccess(adminRoleUsersService.hasById(t.getUserId()));
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("值重复");
        }
    }

    //获取所有信息
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/hasAll")
    public ServerResponse<?> hasAll() {
        log.info("in to hasAll");
        return ServerResponse.createBySuccess(adminRoleUsersService.hasAll());

    }


    //根据ID获取信息
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/hasAllById")
    public ServerResponse<?> hasAllById(HttpServletRequest request) {
        String id = request.getParameter("id");
        log.info("id:" + id);
        return ServerResponse.createBySuccess(adminRoleUsersService.hasAllById(id));

    }

}
